<template>
  <div id="myPurchase-operate" >
    <a-spin :spinning="spinning">
    <div class="filter-box" style="position: relative;" v-for="(item,index) in Data" :key="index">
      <a-button class="qrBtn" type="primary" v-if="item.recStatus==='N' || item.recStatus==='R'" @click="confirmOrder(item)">确认收货</a-button>
      <div class="top-box2" v-if="item.recStatus==='N' || item.recStatus==='R'">
          <div>
            <div class="box-title">收货照片</div>
            <div class="Upload_box">
              <Upload :defaultList="item.receiveFiles" @change="(file)=>handleUploadChange(file,item.receiveFiles,index)" @remove='(file)=>handleOtherRemove(file,item.receiveFiles,index)' :limit="8"
            />
            </div>
          </div>
      </div>
      <div class="top-box2" v-if="item.recStatus==='R'">
          <div>
            <div class="box-title">商议内容</div>
            <div style="padding:20px 0">
              处理时间:{{item.recTime}}
              <span style="padding-left:20px">商议结果:拒绝</span>
              <span style="padding-left:20px">备注:{{item.descr}}</span>
            </div>
          </div>
      </div>
      <div class="top-box">
          <div class="box-title">发货信息</div>
          <!-- <a-button type="primary" v-if="item.recStatus==='N'" @click="confirmOrder(item)">确认收货</a-button> -->
      </div>
      <a-row class="info">
        <a-col :span="8">
            <span class="info-left">发货时间：<span>{{item.sendTime}}</span></span>
        </a-col>
        <a-col :span="8">
            <span class="info-left">发货仓库：{{item.recWarehouseName}}</span>

        </a-col>
        <a-col :span="8">
            <span class="info-left">送货车牌号：{{item.carNum}}</span>

        </a-col>
      </a-row>
      <a-row class="info">
        <a-col :span="8">
            <span class="info-left">司机驾驶证照片：</span>
            <Upload
              :defaultList="item.driverFiles"
              disabled
              :limit="0"
            />
        </a-col>
        <a-col :span="8">
            <span class="info-left">其他照片：</span>
            <Upload
              :defaultList="item.files"
              disabled
              :limit="0"
            />
        </a-col>
      </a-row>
      <div class="container">
      <div class="container-title">发货明细</div>
      <div class="title-info">
        <!-- <span class="info-left">项目：</span><span class="info-right">{{ncProjectName}}</span>
        <span class="info-left">所属组织：</span><span>{{orgName}}</span> -->
      </div>
      <div class="order-table">
        <a-row class="table-header">
          <a-col :span="8" class="header-item">商品</a-col>
          <a-col :span="4" class="header-item">调出单价</a-col>
          <a-col :span="4" class="header-item">调出数量</a-col>
          <a-col :span="4" class="header-item">调入单价</a-col>
          <a-col :span="4" class="header-item">调入数量</a-col>
        </a-row>
      </div>

      <div
          class="table-item"
          v-for="(item, index) in item.details"
          :key="index"
        >
          <a-row class="table-item-body">
            <a-col :span="8" class="flex-row">
              <img :src="item.goodsPic" class="table-pic" />
              <div class="table-info">
                <p class="info-name">{{ item.goodsName }}</p>
                <p>规格：{{ item.goodsSpec }}</p>
                <p>单位：{{ item.goodsUnit }}</p>
              </div>
            </a-col>
            <a-col :span="4" class="item-col">
              ￥{{ item.sendPrice }}
            </a-col>
            <a-col :span="4" class="item-col">
              {{ item.sendNumber }}
            </a-col>
            <a-col :span="4">
              <a-input-number
                v-model="item.recPrice"
                placeholder="请输入收货单价"
              />
            </a-col>
            <a-col :span="4">
              <a-input-number
                v-model="item.recNumber"
                placeholder="请输入收货数量"
              />
            </a-col>

          </a-row>
        </div>
        <div class="table-empty" v-if="!Data.length">
          <img src="@/assets/images/empty.png" />
          <span>暂无数据</span>
        </div>
      <!-- <div class="page-box">
        <a-pagination
          size="small"
          :total="pageConfig.total"
          show-quick-jumper
          :show-total="(total) => `总共 ${pageConfig.total} 条数据`"
          @change="changePage"
        />
      </div> -->
    </div>
    </div>
    </a-spin>
    <a-modal
      v-model="showVisible"
      title=""
      cancel-text="取消"
      ok-text="返回列表"
      style="text-align: center;"
      @ok="handleOk"
      @cancel="handleCancel">
      <p class="model-icon"><a-icon type="check-circle" class="icon" /></p>
      <p class="model-text">收货成功</p>
      <p>非直调材料领用单：{{isScuss}}</p>
    </a-modal>

    <a-modal v-model="showVisible2" title="商议提交" @ok="handleTalkPriceOk">
      <p>调入单价及调入数量需卖家商议确认</p>
      <div>
        <div style="padding-bottom:6px">商议备注</div>
        <a-textarea placeholder="请输入备注" :rows="4" v-model="isTalkPriceData.memo"/>
      </div>
    </a-modal>
  </div>
</template>
<script lang='ts'>
import { Validate } from '@/decorator'
import { Color } from '@antv/attr';
import { Component, Watch, Vue } from 'vue-property-decorator'
import { config } from 'vue/types/umd';

@Component
export default class aduit extends Vue {
  Data: any = [];
  projects: any = [] // 购入项目列表
  warehouses: any = [] // 购入仓库
  checkGoodsList: any = [] // 选中的商品列表
  ncProjectName: any = '' // 项目名称
  orgName: any = '' // 组织名称
  checkAll:boolean= false
  showVisible:boolean= false
  spinning:boolean= false
  defaultList: any[] = []
  showVisible2:boolean = false
  isScuss:any = ''
  isTalkPriceData:any = {
    memo: ''
  }
  get shopOrderTitleId () {
    return this.$route.query.shopOrderTitleId
  }

  init () {
    this.getDetail()
  }
  getDetail () {
    let data = {
      titleId: this.shopOrderTitleId
    }
    this.$api.orderMyDetailFetch(data).then((res: any) => {
      this.Data = res.data.recVOS.filter((item:any) => {
        if (item.recStatus === 'N' || item.recStatus === 'R') {
          return item
        }
      })

      this.Data.forEach((item:any) => {
        // item.receiveFiles = []
        item.driverFiles = item.driverFiles.map((item2: any, index: number) => ({
          uid: -index - 1,
          url: item2.fileUrl,
          name: item2.fileUrl
        }))
        item.files = item.files.map((item2: any, index: number) => ({
          uid: -index - 1,
          url: item2.fileUrl,
          name: item2.fileUrl
        }))
        item.details = item.details.map((item2:any) => ({...item2, recPrice: item2.sendPrice, recNumber: item2.sendNumber}))
        item.receiveFiles = item.receiveFiles.map((item2: any, index: number) => ({
          uid: -index - 1,
          url: item2.fileUrl,
          name: item2.fileUrl
        }))
      });
      console.log(this.Data, 'this.Datathis.Datathis.Datathis.Data')
    });
  }
  handleUploadChange (fileList: any, list:any, index:any) {
    this.Data[index].receiveFiles = fileList
  }
  // 移除收货图片
  handleOtherRemove (fileList: any, list:any, index:any) {
      this.Data[index].receiveFiles = this.Data[index].receiveFiles.filter((item:any) => {
        return item.url !== fileList.url;
      })
  }
  // 确认收货
  confirmOrder (arr:any) {
    let details:any = []
    for (let i = 0; i < arr.details.length; i++) {
      if (arr.details[i].recPrice === 0 || arr.details[i].recPrice === null) return this.$message.error('请输入收货单价')
      if (arr.details[i].recNumber === 0 || arr.details[i].recNumber === null) return this.$message.error('请输入收货数量')
    }
    if (arr.receiveFiles.length == 0) {
      return this.$message.error('请上传图片')
    }

    if (arr.details.some((item:any) => item.sendNumber != item.recNumber || item.recPrice != item.sendPrice)) {
      this.showVisible2 = true
      this.isTalkPriceData = arr
      return
    }
    details = arr.details.map((item2:any) => ({
      recDetailId: item2.recDetailId,
      recPrice: item2.recPrice,
      recNumber: item2.recNumber
      }))
      let receiveFiles = []
      receiveFiles = arr.receiveFiles.map((item:any) => ({fileUrl: item.url}))
    let data = {
      recId: arr.recId,
      details,
      receiveFiles: receiveFiles
    }
  this.spinning = true
    this.$api.orderHadReceive2Fetch(data).then((res) => {
        this.spinning = false
        this.showVisible = true
        this.isScuss = res.data
        // this.$router.back()
    },
    (err) => {
      console.log(err);
      this.spinning = false
    })
  }
  // 弹框确认
  handleOk () {
    this.showVisible = false
    this.$router.back()
    // this.$router.replace({
    //   path: '/allot/myPurchase/detail',
    //   query: {
    //     shopOrderTitleId: this.shopOrderTitleId
    //     }
    //   })
  }
  // 弹框返回
  handleCancel () {
    this.showVisible = false
    this.$router.back()
  }
  // 商议
  handleTalkPriceOk () {
    let receiveFiles = this.isTalkPriceData.receiveFiles.map((item:any) => ({fileUrl: item.url}))
    this.isTalkPriceData.receiveFiles = receiveFiles

    this.$api.orderTalkPriceFetch(this.isTalkPriceData).then(res => {
      if (res.code == 0) {
        this.showVisible2 = false
        this.spinning = false
        this.$message.success('提交成功')
        setTimeout(() => {
          this.$router.back()
        }, 1000);
      }
    }).catch(() => {
      this.spinning = false
    })
  }
}

</script>
<style lang="scss" scoped>
#myPurchase-operate {
    .top-box,.top-box2{
      margin-bottom: 15px;
        .box-title{
            display: flex !important;
            font-size: 16px;
            font-weight: 700;
            padding-left: 5px;
            color: #000;
            height: 15px;
            line-height: 15px;
            border-left: 3px solid rgb(62, 149, 231);
        }
    }
    .info{
        padding: 10px 10px;
        .address{
          display: flex;
        }
        .info-left{
            width: 65px;
            margin-right: 10px;

        }
    }
    .container-title{
      font-size: 16px;
      font-weight: 700;
      padding-left: 5px;
      color: #000;
      height: 15px;
      line-height: 15px;
      border-left: 3px solid rgb(62, 149, 231);
    }
    .title-info{
      margin: 20px 0;
      font-size: 16px;
      .info-left{
        font-weight: 700;
      }
      .info-right{
        margin-right: 80px;
      }
    }
    .header-item{
      text-align: center;
    }
    .table-item{
      .table-item-body {
        border-bottom: 1px solid #F1F1F1;
        // border-top: 1px solid #F1F1F1;
        // margin-bottom: 16px;
        .ant-col {
            height: 152px;
            padding: 16px 20px;
            border-right: 1px solid #F1F1F1;
            display: flex;
            flex-direction: column;
            justify-content: center;
            color: #333;
            &:last-child {
                border-right: none;
            }
            p {
                margin-bottom: 0;
            }
            .table-pic {
                max-width: 160px;
                width: 50%;
                height: 120px;
                display: block;
                object-fit: cover;
                margin-right: 24px;
            }
            .table-info {
                display: flex;
                flex-direction: column;
                p {
                    font-size: 13px;
                    color: #999;
                }
                .info-name {
                    // @include limitLine(2);
                    margin-bottom: 16px;
                    font-size: 14px;
                    color: #000;
                    font-weight: 700;
                    // cursor: pointer;
                    // &:hover {
                    //     text-decoration: underline;
                    // }
                }
            }
        }
        .flex-row {
            flex-direction: row;
            align-items: center;
            justify-content: flex-start;
            .ant-btn {
                margin-right: 12px;
                font-size: 12px;
            }
        }
        .item-col{
          text-align: center;
        }

    }
  }
}
.model-icon{
  color: green;

  .icon{
    font-size: 50px;
  }
}
.model-text{
  font-weight: 700;
  font-size: 16px;
}
.container{
  margin: 0;
  padding: 0;
  margin-top: 30px;
  padding-bottom: 30px;
  height: 450px;
  overflow-x: hidden;
	overflow-y: scroll;
}
.container::-webkit-scrollbar{
	display: none;
}
.qrBtn{
  position: absolute;
  top: 10px;
  right: 10px;
}
.Upload_box{
  padding-top: 10px;
}
</style>
